layui.use(['form','layer','upload','laydate','treeSelect','selectN'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;

    var treeSelect= layui.treeSelect,//树形下拉框
        selectN=layui.selectN;


    var date=new Date();var year = date.getFullYear();
    let years = [];
    for(let i=year;i>=2000;i--){
        years.push({"id":`${i}`,"name":`${i}`+"级"});
    }
    var catData = [{"id":1,"name":"教师"},{"id":2,"name":"学生","children":years}];
    //无限级分类-所有配置
    var catIns = null;

//通过js动态选择
//     $('.set1').click(function(){
//         catIns1.set([6,10]);
//
//     });

    //监听重置按钮
    $('form').find(':reset').click(function(){
        $('form')[0].reset();
        $("#roleName").val(tempRole);//重置后身份不能改变
        catIns.set();//默认值
        return false;
    });


    var tempRole="visitor";

    var tempLoginName="";


    //回显数据
    var request = GetRequest();
    $.get("/admin/user/" + request.id,function (user) {

        $("#id").val(user.id);
        $("#password").val(user.password);

        $("#loginName").val(user.loginName);
        tempLoginName=user.loginName;
        $("#roleId").val(user.roleId);

        $("#realname").val(user.realname);

        $("#sex input[value="+user.sex+"]").prop("checked",true);
        form.render('radio');

        $("#mobile").val(user.mobile);
        $("#email").val(user.email);
        $("#remarks").text(user.remarks);

        $("#userType").val(user.userType);
        if (user.userType == "0") {
            $("#userType").prop("checked",true);
        } else {
            $("#userType").prop("checked",false);
        }
        form.render();
        $.ajax({
            url: '/admin/role/'+user.roleId,
            type: 'GET',
            success: function (da) {
                if(da!=null) {
                    tempRole=da.name;
                    treeSelect.render({
                        elem: '#tree-select',
                        data: '/role/getAll',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(data){
                            $("#tree-select").val(data.current.id);
                        },
                        success: function (data) { // 加载完成后的回调函数
                             $("#tree-select").val(da.id);
                        }
                    });
                }
            }})

        catIns=selectN({
            //元素容器【必填】
            elem: '#realname'
            //候选数据【必填】
            ,data: catData
            //数据的键名
            // ,field:{idName:'id',realName:'name',age:'children'}
            //空值项提示，可设置为数组['请选择省','请选择市','请选择县']
            ,tips: ['请选择','请选择年级']
            ,selected:user.realname==1?user.realname:[user.realname,user.age]
            //设置了长度
            // ,width:null
            // //默认值
            // ,selected: [6,10]
            // //为真只取最后一个值
            // ,last:true
            //
            // //事件过滤器，lay-filter名 不设置与选择器相同(去#.)
            // ,filter: ''
            //
            // //input的name 不设置与选择器相同(去#.)
            // ,name: 'cat2'
            //
            // //数据分隔符
            // ,delimiter: ','
            //表单区分 form.render(type, filter); 为class="layui-form" 所在元素的 lay-filter="" 的值
            // ,formFilter: null

        });
    });

    //监听是否启用角色开关
    form.on('switch(switchTest)', function(data){
        if (this.checked) {
            $("#userType").val("0")
        } else {
            $("#userType").val("1")
        }
    });


    //修改用户分类信息
    form.on('submit(edit)', function(data){
        if(data.field.loginName!=tempLoginName) {
            $.ajax({
                url: "/verifyUserName",
                type: "POST",
                data:{loginName:data.field.loginName},
                success: function (d) {
                    if (d.code != 200) {

                        data.field.userType =  $("#userType").val();
                        //处理realname数据
                        var ids=data.field.realname.split(",");
                        if(ids.length!=0) {
                            data.field.realname = ids[0];
                            if (ids.length == 2)
                                data.field.age = ids[1];
                            else
                                data.field.age = "";
                        }
                        else
                            data.field.realname="";

                        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                        $.ajax({
                            url: '/admin/user/edit',
                            data: data.field,
                            type: 'post',
                            success: function (data) {
                                if(data.code == 200) {
                                    top.layer.close(index);
                                    top.layer.msg("修改成功！");
                                    layer.closeAll("iframe");
                                    //刷新父页面
                                    parent.location.reload();
                                } else {
                                    top.layer.close(index);
                                    top.layer.msg("修改失败！");
                                }
                            },
                            error:function () {
                                top.layer.close(index);
                                top.layer.msg("修改失败！");
                            }
                        });

                    } else {
                        layer.msg('修改失败！该用户名已存在！');

                    }
                }
            })
        }else {

            data.field.userType =  $("#userType").val();
            //处理realname数据
            var ids=data.field.realname.split(",");
            if(ids.length!=0) {
                data.field.realname = ids[0];
                if (ids.length == 2)
                    data.field.age = ids[1];
                else
                    data.field.age = "";
            }
            else
                data.field.realname="";

            var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.ajax({
                url: '/admin/user/edit',
                data: data.field,
                type: 'post',
                success: function (data) {
                    if(data.code == 200) {
                        top.layer.close(index);
                        top.layer.msg("修改成功！");
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    } else {
                        top.layer.close(index);
                        top.layer.msg("修改失败！");
                    }
                },
                error:function () {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            });

        }



        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});